package com.wu.llpsw.item.wushu;


import com.wu.llpsw.data.playerData;
import com.wu.llpsw.entity.model.fireballEntity;
import com.wu.llpsw.networking.ModMessage;
import com.wu.llpsw.networking.packet.backYuanLiMaxPacket;
import com.wu.llpsw.networking.packet.backYuanLiNowPacket;
import com.wu.llpsw.yuanli.energy.yuanliManager;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Predicate;

public class jiu_you_jian extends BowItem {
    public float attackValue=10.0f;//基础攻击力
    public int setup=100;//启动技能需要的最少元力
    int timeLong=1000;
    public jiu_you_jian(Properties p_41383_) {
        super(p_41383_);
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level p_40672_, Player p_40673_, InteractionHand p_40674_) {
        ItemStack itemstack = p_40673_.getItemInHand(p_40674_);
        p_40673_.startUsingItem(p_40674_);
        return InteractionResultHolder.pass(itemstack);
    }


    //添加描述
    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> p_41423_, TooltipFlag p_41424_) {
        p_41423_.add(new TranslatableComponent("tooltip.wu.jiu_you_jian.tooltip"));
    }


    @Override
    public int getUseDuration(ItemStack p_40680_) {
        return timeLong;
    }


    @Override
    public Predicate<ItemStack> getAllSupportedProjectiles() {
        return ARROW_ONLY;
    }

    //使用弓的动画
    @Override
    public UseAnim getUseAnimation(ItemStack p_40678_) {
        return UseAnim.BOW;
    }

    //获取使用时间，蓄力发动技能
    @Override
    public void releaseUsing(ItemStack p_41412_, Level world, LivingEntity entity, int time) {
        if (!world.isClientSide){
            if(entity instanceof Player){
                int out=timeLong-time;
                out+=setup;
                Player player=(Player) entity;
                if(yuanliManager.getNowYuanLi(player)>=out){
                    yuanliManager.use(player,-out);

                    //生成粒子效果
                    ServerLevel serverWorld = (ServerLevel) player.getCommandSenderWorld();
                    double x1 = player.getX();
                    double y1 = player.getY()+1.0f;
                    double z1 = player.getZ();
                    double xa= player.getLookAngle().x;
                    double ya= player.getLookAngle().y;
                    double za= player.getLookAngle().z;
                    for (float i=0.0f;i<40.0f;i+=2.0f){
                        serverWorld.sendParticles(ParticleTypes.SOUL, x1+xa*i, y1+ya*i, z1+za*i, 7, 1.0f, 0, 0, 0);
                        serverWorld.sendParticles(ParticleTypes.SWEEP_ATTACK, x1+xa*i, y1+ya*i, z1+za*i, 7, 0.0f, 0, 0, 0);
                    }
                    //创建火球实体
                    fireballEntity fireball=new fireballEntity(player.getCommandSenderWorld(),player,x1,y1+0.8f,z1,(float) (playerData.getAttack(player)+attackValue+out*0.1f));
                    //加入世界
                    world.addFreshEntity(fireball);
                    //发射火球
                    fireball.setDeltaMovement(xa*10,ya*10,za*10);
                    player.addEffect(new MobEffectInstance(MobEffects.GLOWING, 20 * 5, 1));
                    player.getCooldowns().addCooldown(this,20*4);
                }
            }
        }
    }
}
